package com.ia.aistream.manager.operating.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 * 存放Doc文档解析的正则方法
 * @author XiaoYu
 */
public class DocRegularUtil {

    private DocRegularUtil(){}

    /***
     * 验证是否为附录信息
     * @param msg
     * @return
     */
    public static boolean isAppendix(String msg){
        String reg = "^\\d\\.?\\s*附录";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        return matcher.matches();
    }

    /**********************  单元格内容的正则  **********************************/
    /**
     * 处理单元格中设备分行的情况
     * @param msg
     * @return
     */
    public static boolean isTableCellContent(String msg){
        String reg = "^[A-Z0-9/]*\\d{3,4}/_\\d{3,4}[A-Z0-9/]*$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        return matcher.matches();
    }

    /**********************  处理位置信息正则  *****************************/
    /***
     * 判断数据是否为厂房
     * @param msg
     * @return
     */
    public static boolean isTwoLetter(String msg){
        String reg = "^[A-Z]{2}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);
        // 去掉特殊数据
        if(matcher.matches() && !msg.equals("VH") && !msg.equals("VL") && !msg.equals("ON")){
            return true;
        }
        return false;
    }
    /***
     * 判断是否符合位置的正则表达式
     * @param msg
     * @return
     */
    public static boolean isLocation(String msg){
        String equPattern = "^[A-Z]{1,2}\\d{3}$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /***
     * 1MX-7.5m
     * 验证字符串是否符合位置信息
     * @param msg
     * @return
     */
    public static boolean isCrewLocation(String msg){

        String reg = "^\\d?[A-Z]{2}-?\\s*\\d+\\.?\\d*[a-z]?米?$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        if(matcher.matches() && msg.length()>3){
            return true;
        }
        return false;
    }

    /**********************  处理参数的正则表达式     *******************************************/
    /**
     * 参数是否存在字母
     * @param msg
     * @return
     */
    public static boolean containsLetter(String msg){
        String equPattern = "^.*[a-zA-Z]+.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /**
     * 判断是否为单位
     * @param s
     * @return
     */
    public static boolean isUnit(String s){

        if(s.endsWith("A")){
            return true;
        }
        // 处理是否为单位
        String reg = "^.*\\d{1}A[^A-Z]+$";
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(s);

        return matcher.matches();
    }
    /***
     * 处理参数
     * @param msg
     * @return
     */
    public static boolean handleNum(String msg){
        msg = msg.trim();
        String equPattern = "^-?\\d+\\.?/?±?\\d*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /***
     * 判断是否符合0.2-0.7MPa的情况
     * @param msg
     * @return
     */
    public static boolean isMiddleUnit(String msg){
        String equPattern = "^.*\\d+\\.?\\d*-\\d+\\.?\\d*[a-zA-Z]*.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /**
     * 验证是否符合该格式2<P<15kPa
     * @param msg
     * @return
     */
    public static boolean isMiddleParam(String msg){
        String equPattern = "^.*\\d+<[a-zA-Z]*<\\d+.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /***
     * 判断是否为dd-dd类型的参数
     * @param msg
     * @return
     */
    public static boolean isInterval(String msg){
        String equPattern = "^\\d+\\.?\\d*-\\d+\\.?\\d*|\\d+\\.?\\d*~\\d+\\.?\\d*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }


    /**********************  处理设备的特殊正则表达式  **************************************/
    /***
     * 处理001 MD/L情况
     * @param msg
     * @return
     */
    public static boolean isThreeEquipment(String msg){
        msg = msg.replace(" ", "");
        String reg = "^\\d{3,5}[A-Z]{2}/[A-Z]{1}";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);
        return matcher.matches();
    }
    /***
     * 除氧器水位调节阀025、026VL保持；
     * @param msg
     * @return
     */
    public static boolean isNumOther(String msg){
        msg = msg.replace(" ", "");
        String equPattern = "^.*\\d{2}、\\d{3}[A-Z]+.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /***
     * 处理情况1CEX201或301或101MP
     * @param msg
     * @return
     */
    public static boolean isOrEquipment(String msg){
        String equPattern = "^.*\\d*[A-Z]*或\\d{1}.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        if(equM.matches() && msg.length()>5){
            return true;
        }
        return false;
    }
    /***
     * 判断系统设备编码含有‘-’和‘/’分割的情况
     * @param msg
     * @return
     */
    public static boolean isNeedEquipment(String msg){
        String equPattern = "^.*\\d*[A-Z]*/\\d{1}.*|.*\\d{2}-\\d{2,5}[A-Z]{2}.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        if(equM.matches() && msg.length()>5){
            return true;
        }
        return false;
    }
    /***
     * 1 ATE 443/444LP 情况
     * @param msg
     * @return
     */
    public static boolean isNormalEquipment(String msg){
        String equPattern = "^.*\\d{2}/\\d{2}.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /**
     * 判断是否为这种情况1CEX320VL/022VD
     * @param msg
     * @return
     */
    public static boolean isOtherEquipment(String msg){
        String equPattern = "^.*[A-Z]+/\\d{2}.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);
        String[] split = msg.split("/");
        if(split.length==2 && equM.matches()){
            return true;
        }
        return false;
    }
    /***
     *    205 KT—210 KT
     * @param msg
     * @return
     */
    public static boolean isTwoEqu(String msg){
        String equPattern = "^.*\\d{3,4}[A-Z]+—\\d{3,4}[A-Z]+.*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /***
     * 001PO/002/003PO置于手动
     * @param msg
     * @return
     */
    public static boolean isThrewEquipment(String msg){
        String reg = "\\d{3,4}[A-Z]{2}/\\d{3,4}/\\d{3}[A-Z]{2}[a-zA-Z]?";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        return matcher.matches();
    }


    /*********************  验证设备编码工具类  **************************/
    /**
     * 处理5位设备码
     * @param msg
     * @return
     */
    public static boolean getEquipment(String msg){
        msg = msg.replace(" ", "");
        String equPattern = "^\\d{3,4}[A-Z]{1}[a-zA-Z]{1,2}\\d?$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);

        return equM.matches();
    }
    /***
     * 处理九位设备码
     * @param msg
     * @return
     */
    public static String handleSystemEquipment(String msg){
        msg = msg.trim();
        String equPattern = "^\\d?\\s?[A-Z]{3}\\s?\\d{3,5}\\s?[a-zA-Z]{2,}\\s?\\d?[a-zA-Z]*$";
        Pattern equR = Pattern.compile(equPattern);
        Matcher equM = equR.matcher(msg);
        if(equM.matches()){
            return msg;
        }
        return null;
    }
    /***
     * 判断是否为机组系统
     * @param msg
     * @return
     */
    public static boolean isSystem(String msg){
        String reg = "\\d?\\s?[A-Z]{3}";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        // 处理特殊情况
        if(matcher.matches() && !msg.equals("OFF")){
            return true;
        }

        return false;
    }


    /******************************  其他类型正则  **********************************/
    /***
     * 判断是否为引用文件编码
     * @param msg
     * @return
     */
    public static boolean isFileCode(String msg){

        String reg = ".*-[A-Z]+-.*";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);
        return matcher.matches();
    }
}
